package io.fotoapparat.facedetector.app;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.util.List;
import io.fotoapparat.Fotoapparat;
import io.fotoapparat.FotoapparatSwitcher;
import io.fotoapparat.facedetector.Rectangle;
import io.fotoapparat.facedetector.processor.FaceDetectorProcessor;
import io.fotoapparat.facedetector.view.RectanglesView;
import io.fotoapparat.parameter.LensPosition;
import io.fotoapparat.view.CameraView;
import static io.fotoapparat.log.Loggers.*;
import static io.fotoapparat.parameter.selector.LensPositionSelectors.lensPosition;

public class MainActivity extends AppCompatActivity
{
	private final PermissionsDelegate mDelegate=new PermissionsDelegate(this);
	private boolean mHasPermission;
	private CameraView vCamera;
	private RectanglesView vRects;
	private FotoapparatSwitcher mSwitcher;
	private Fotoapparat mFotoF;
	private Fotoapparat mFotoB;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		vCamera=(CameraView)findViewById(R.id.camera_view);
		vRects=(RectanglesView)findViewById(R.id.rectanglesView);
		mHasPermission=mDelegate.hasCameraPermission();
		if(mHasPermission) vCamera.setVisibility(View.VISIBLE);
		else mDelegate.requestCameraPermission();
		mFotoF=createFotoapparat(LensPosition.FRONT);
		mFotoB=createFotoapparat(LensPosition.BACK);
		mSwitcher=FotoapparatSwitcher.withDefault(mFotoB);
		View switchCameraButton=findViewById(R.id.switchCamera);
		switchCameraButton.setVisibility(canSwitchCameras()? View.VISIBLE: View.GONE);
		switchCameraButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v){
				switchCamera();
			}
		});
	}
	
	private boolean canSwitchCameras(){
		return mFotoF.isAvailable()==mFotoB.isAvailable();
	}
	
	private Fotoapparat createFotoapparat(LensPosition position){
		return Fotoapparat.with(this)
				.into(vCamera)
				.lensPosition(lensPosition(position))
				.frameProcessor(FaceDetectorProcessor.with(this)
						.listener(new FaceDetectorProcessor.OnFacesDetectedListener()
						{
							@Override
							public void onFacesDetected(List<Rectangle> faces){
								if(mSwitcher.getCurrentFotoapparat()==mFotoF){
									//前置相机，识别框需要水平翻转
									for(int i=0, j=faces.size(); i<j; i++){
										Rectangle r=faces.get(i);
										float width=r.getWidth();
										r=new Rectangle(
												(1-r.getX()-width),
												r.getY(),
												width,
												r.getHeight()
										);
										faces.set(i, r);
									}
								}
								vRects.setRectangles(faces);
							}
						})
						.build())
				.logger(loggers(logcat(), fileLogger(this)))
				.build();
	}
	
	private void switchCamera(){
		if(mSwitcher.getCurrentFotoapparat()==mFotoF){
			mSwitcher.switchTo(mFotoB);
		}else{
			mSwitcher.switchTo(mFotoF);
		}
	}
	
	@Override
	protected void onStart(){
		super.onStart();
		if(mHasPermission){
			mSwitcher.start();
		}
	}
	
	@Override
	protected void onStop(){
		super.onStop();
		if(mHasPermission){
			mSwitcher.stop();
		}
	}
	
	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
			@NonNull int[] grantResults){
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		if(mDelegate.resultGranted(requestCode, permissions, grantResults)){
			mSwitcher.start();
			vCamera.setVisibility(View.VISIBLE);
		}
	}
}
